home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C25 / Visitor.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.1 KB  |  41 lines

  1. //: C25:Visitor.h
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // The base interface for visitors
  7. // and template for visitable Trash types
  8. #ifndef VISITOR_H
  9. #define VISITOR_H
  10. #include "Trash.h"
  11. #include "Aluminum.h"
  12. #include "Paper.h"
  13. #include "Glass.h"
  14. #include "Cardboard.h"
  15.  
  16. class Visitor {
  17. public:
  18.   virtual void visit(Aluminum* a) = 0;
  19.   virtual void visit(Paper* p) = 0;
  20.   virtual void visit(Glass* g) = 0;
  21.   virtual void visit(Cardboard* c) = 0;
  22. };
  23.  
  24. // Template to generate visitable 
  25. // trash types by inheriting from originals:
  26. template<class TrashType> 
  27. class Visitable : public TrashType {
  28. protected:
  29.   Visitable () : TrashType(0) {}
  30.   friend class TrashPrototypeInit;
  31. public:
  32.   Visitable(double wt) : TrashType(wt) {}
  33.   // Remember "this" is pointer to current type:
  34.   void accept(Visitor& v) { v.visit(this); }
  35.   // Override clone() to create this new type:
  36.   Trash* clone(const Trash::Info& info) {
  37.     return new Visitable(info.data());
  38.   }
  39. };
  40. #endif // VISITOR_H ///:~
  41.